package com.yunwushanhe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunwushanhe.bean.req.LinkAddReq;
import com.yunwushanhe.bean.req.LinkPageListReq;
import com.yunwushanhe.bean.req.LinkStatusUpdateReq;
import com.yunwushanhe.bean.req.LinkUpdateReq;
import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.LinkPageListVO;
import com.yunwushanhe.bean.vo.LinkUpdateVO;
import com.yunwushanhe.bean.vo.LinkVO;
import com.yunwushanhe.bean.vo.PageVO;
import com.yunwushanhe.constant.Constant;
import com.yunwushanhe.mapper.LinkMapper;
import com.yunwushanhe.entity.Link;
import com.yunwushanhe.service.LinkService;
import com.yunwushanhe.util.BeanCopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListResourceBundle;

/**
 * 友链(Link)表服务实现类
 *
 * @author 抬头看看天
 * @since 2023-08-05 21:43:19
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult<List<LinkVO>> getAllLink() {
        LambdaQueryWrapper<Link> qw = new LambdaQueryWrapper<>();
        qw.eq(Link::getStatus, Constant.LINK_STATUS_NORMAL);
        List<Link> links = list(qw);
        List<LinkVO> linkVOS = BeanCopyUtil.copyBeanList(links, LinkVO.class);
        return ResponseResult.ok(linkVOS);
    }

    @Override
    public ResponseResult<PageVO<List<LinkPageListVO>>> getLinkPageList(LinkPageListReq linkPageListReq) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(linkPageListReq.getName()),
                Link::getName,linkPageListReq.getName());
        queryWrapper.eq(StringUtils.hasText(linkPageListReq.getStatus()),
                Link::getStatus,linkPageListReq.getStatus());

        Page<Link> linkPage = new Page<>();
        linkPage.setCurrent(linkPageListReq.getPageNum());
        linkPage.setSize(linkPageListReq.getPageSize());

        page(linkPage,queryWrapper);
        List<LinkPageListVO> linkPageListVOList =
                BeanCopyUtil.copyBeanList(linkPage.getRecords(), LinkPageListVO.class);
        PageVO<List<LinkPageListVO>> pageVO = new PageVO<>(linkPageListVOList, linkPage.getTotal());
        return ResponseResult.ok(pageVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> addLink(LinkAddReq linkAddReq) {
        Link link = BeanCopyUtil.copyBean(linkAddReq, Link.class);
        save(link);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult<LinkUpdateVO> getLinkUpdateById(Long id) {
        Link link = getById(id);
        LinkUpdateVO linkUpdateVO = BeanCopyUtil.copyBean(link, LinkUpdateVO.class);
        return ResponseResult.ok(linkUpdateVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> updateLinkById(LinkUpdateReq linkUpdateReq) {
        HashMap<String, Object> eqMap = new HashMap<>();
        eqMap.put("id",linkUpdateReq.getId());
        UpdateWrapper<Link> updateWrapper = BeanCopyUtil.ignoreNullUpdateWrapper(eqMap, linkUpdateReq, Link.class);
        update(updateWrapper);
        return ResponseResult.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> deleteLinkById(String id) {
        String[] arrayId = id.split(",");
        ArrayList<Long> listId = new ArrayList<>();
        for (String s : arrayId) {
            listId.add(Long.valueOf(s));
        }

        removeByIds(listId);
        return ResponseResult.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> changeLinkStatus(LinkStatusUpdateReq linkStatusUpdateReq) {
        try {
            HashMap<String, Object> eqMap =
                    BeanCopyUtil.eqMap("id", linkStatusUpdateReq.getId());
            UpdateWrapper<Link> updateWrapper =
                    BeanCopyUtil.ignoreNullUpdateWrapper(eqMap, linkStatusUpdateReq, Link.class);
            update(updateWrapper);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return ResponseResult.ok();
    }
}

